package com.jpxx.netty.chaibao;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

  
public class ServerHandler extends ChannelHandlerAdapter{  
		@Override  
	    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {  
	    	 String request = (String)msg;//收到Client的请求转为String
	    	 System.out.println("Server:"+request);
	    	 String response="---我是响应数据$_被断开的数据$_";
	    	 //冲刷回管道里面的时候还得是buf类型
	         ctx.writeAndFlush(Unpooled.copiedBuffer(response.getBytes()));
	         //.addListeners(ChannelFutureListener.CLOSE);
	    }  

		@Override  
	    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {  
	        cause.printStackTrace();  
	        ctx.close();  
	    }

		@Override
		public void channelActive(ChannelHandlerContext ctx) throws Exception {
			// TODO Auto-generated method stub
			System.out.println("客户端连接通道");
			super.channelActive(ctx);
		}  
 
	
    
} 